package id.ac.amikom.amikomsocial;

import java.util.ArrayList;
import java.util.HashMap;

import com.markupartist.android.widget.PullToRefreshListView;
import com.markupartist.android.widget.PullToRefreshListView.OnRefreshListener;

import id.ac.amikom.libs.AdemDBHelper;
import id.ac.amikom.libs.AdemServiceHelper;
import id.ac.amikom.libs.SpecialAdapter;
import android.app.Dialog;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ListAdapter;

public class AdemCalendarActivity extends ListActivity {

	private AdemDBHelper db = null;

	private class GetDataTask extends AsyncTask<Void, Void, Void> {

		@Override
		protected Void doInBackground(Void... params) {

			//Cursor c = db.getLogin();
			//if (c.getCount() > 0) {
				AdemServiceHelper srv = new AdemServiceHelper(
						AdemCalendarActivity.this);
				if (srv.checkCalendarService())
					srv.getCalendarService();
			//}

			return null;

		}

		@Override
		protected void onPostExecute(Void result) {

			//Cursor c = db.getLogin();
			//if (c.getCount() > 0)
				setList();
			//else
			//	Toast.makeText(
			//			AdemCalendarActivity.this,
			//			"Refresh Calendar Failed, Please Login from Menu Setting",
			//			Toast.LENGTH_LONG).show();

			((PullToRefreshListView) getListView()).onRefreshComplete();
			super.onPostExecute(result);
		}
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.adem_list);

		((PullToRefreshListView) getListView())
				.setOnRefreshListener(new OnRefreshListener() {
					public void onRefresh() {
						new GetDataTask().execute();
					}
				});

		db = new AdemDBHelper(this);
		//Cursor c = db.getLogin();

		//if (c.getCount() > 0)
			setList();
		//else
		//	Toast.makeText(AdemCalendarActivity.this,
		//			"Refresh Calendar Failed, Please Login from Menu Setting",
		//			Toast.LENGTH_LONG).show();

		//c.close();
		db.close();
	}

	public void setList() {
		try {

			Cursor cr = db.getCalendar();
			startManagingCursor(cr);

			ArrayList<HashMap<String, String>> infoList = new ArrayList<HashMap<String, String>>();

			if (cr.moveToFirst()) {
				do {

					String[] ruang = cr
							.getString(cr.getColumnIndex("location")).split(
									"\\-+");
					String[] st = cr.getString(cr.getColumnIndex("start"))
							.split("\\s+");
					String[] en = cr.getString(cr.getColumnIndex("end")).split(
							"\\s+");

					HashMap<String, String> map = new HashMap<String, String>();
					map.put("title", cr.getString(cr.getColumnIndex("title"))
							.toUpperCase() + " (" + ruang[1].trim() + ")");
					map.put("date",
							st[1] + "-" + en[1] + " * "
									+ cr.getString(cr.getColumnIndex("detail")));
					infoList.add(map);
				} while (cr.moveToNext());
			}

			ListAdapter adapter = new SpecialAdapter(this, infoList,
					R.layout.adem_list, new String[] { "title", "date" },
					new int[] { R.id.list_title, R.id.list_date });

			setListAdapter(adapter);
			registerForContextMenu(getListView());

			cr.close();
			db.close();

		} catch (Exception e) {
			Log.e("Exception found ", e.getMessage());
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.layout.adem_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {
		case R.id.id_login:
			// new GetDataTask().execute();
			startActivity(new Intent(AdemCalendarActivity.this,
					AdemLoginActivity.class));
			finish();

			return true;
		case R.id.id_setting:
			startActivity(new Intent(AdemCalendarActivity.this,
					AdemSettingActivity.class));
			finish();

			return true;
		case R.id.id_about:
			Dialog dialog = new Dialog(this);
			dialog.setContentView(R.layout.adem_info);
			dialog.setTitle("Amikom Social");
			dialog.setCancelable(true);
			dialog.show();

			return true;
		default:
			return super.onOptionsItemSelected(item);
		}

	}

}
